package com.huicoo.forestmanager.ui.patrol.fireAlarm.presenter;


import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.huicoo.forestmanager.network.bean.alarm.AlarmBean;
import com.huicoo.forestmanager.network.bean.patrol.AttachmentEntity;
import com.huicoo.forestmanager.network.bean.patrol.UploadFileBean;
import com.huicoo.forestmanager.ui.patrol.fireAlarm.contract.AlarmContract;
import com.huicoo.forestmanager.ui.patrol.fireAlarm.model.AlarmModel;
import com.huicoo.forestmanager.util.JsonUtils;
import com.huicoo.forestmanager.util.LogUtils;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AlarmPresenter implements AlarmContract.Presenter {
    private AlarmContract.View view;
    private AlarmContract.Model model;

    public AlarmPresenter(AlarmContract.View view) {
        this.view = view;
        this.model = new AlarmModel();
    }

    @Override
    public void uploadFile(HashMap<String, String> params, AttachmentEntity attachment) {
        Call<UploadFileBean> call = model.uploadFile(params, attachment);
        call.enqueue(new Callback<UploadFileBean>() {
            @Override
            public void onResponse(@NonNull Call<UploadFileBean> call, @NonNull Response<UploadFileBean> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo upload file result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.fail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200")) {
                        List<UploadFileBean.UploadFileData> Data = response.body().getData();
                        if (Data != null && !Data.isEmpty() && Data.get(0) != null && Data.get(0).IsSuccess) {
                            if (view != null) {
                                view.uploadFileSuccess(Data.get(0), attachment.attachmentType, attachment.during);
                            }
                        } else {
                            if (view != null) {
                                view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                            }
                        }
                    } else {
                        if (view != null) {
                            view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<UploadFileBean> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
                LogUtils.v("gogogo on failure = " + t.getMessage());
                if (view != null) {
                    view.fail("请求失败，请重试");
                }
            }
        });
    }

    @Override
    public void alarm(HashMap<String, String> params) {
        Call<AlarmBean> call = model.alarm(params);
        call.enqueue(new Callback<AlarmBean>() {
            @Override
            public void onResponse(@NonNull Call<AlarmBean> call, @NonNull Response<AlarmBean> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo alarm result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.fail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200") && response.body().getData().CaseID > 0) {
                        if (view != null) {
                            view.alarmSuccess();
                        }
                    } else {
                        if (view != null) {
                            view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<AlarmBean> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
                if (view != null) {
                    view.fail("请求失败，请重试");
                }
            }
        });
    }

    @Override
    public void destroy() {
        view = null;
    }

}
